package com.ohuang.myapp.fragment;

import com.ohuang.baseapp.fragment.Fragment;
import com.ohuang.baseapp.utils.HiLogUtil;
import com.ohuang.baseapp.view.pageSlider.FragmentPagerProvider2;
import com.ohuang.myapp.ResourceTable;
import com.ohuang.myapp.fragment.one.ImgFragment;
import com.ohuang.myapp.fragment.one.ListFragment;
import com.ohuang.myapp.fragment.one.VideoFragment;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends LogFragment {


    @Override
    protected int getXmlId() {
        return ResourceTable.Layout_fg_first;
    }

    public static FirstFragment newInstance() {

        FirstFragment frag = new FirstFragment();

        return frag;
    }


    TabList tabList;
    PageSlider pageSlider;
    Fragment imgFg, listFg, videoFg;
    List<Fragment> fragmentList;
    FragmentPagerProvider2 pageSliderProvider;

    @Override
    protected void initComponent() {

        pageSlider = findComponentById(ResourceTable.Id_page_slider);
        tabList = findComponentById(ResourceTable.Id_tab_list);
        fragmentList = new ArrayList<>();
        imgFg = new ImgFragment();
        listFg = new ListFragment();
        videoFg = new VideoFragment();

        fragmentList.add(imgFg);
        fragmentList.add(listFg);
        fragmentList.add(videoFg);
        fragmentList.add(new ImgFragment());
        fragmentList.add(new ImgFragment());
        fragmentList.add(new ImgFragment());

        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {
                HiLogUtil.debug("PageChangedListener", "onPageSlideStateChanged" + i);

            }

            @Override
            public void onPageChosen(int i) {

                tabList.selectTabAt(i);
                pageSliderProvider.selectAndRemove(i, 1);


            }
        });


        pageSliderProvider = new FragmentPagerProvider2(getChildFragmentManager()) {

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public Fragment getFragment(int i) {
                return fragmentList.get(i);
            }
        };
        pageSlider.setProvider(pageSliderProvider);
//        pageSlider.setPageCacheSize(2);

        TabList.Tab tab1 = tabList.new Tab(getContext());
        tab1.setText("Image");

        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("List");
        TabList.Tab tab3 = tabList.new Tab(getContext());
        tab3.setText("Video");
        TabList.Tab tab4 = tabList.new Tab(getContext());
        tab4.setText("Image");
        TabList.Tab tab5 = tabList.new Tab(getContext());
        tab5.setText("Image");
        TabList.Tab tab6 = tabList.new Tab(getContext());
        tab6.setText("Image");


        tabList.addTab(tab1);
        tabList.addTab(tab2);
        tabList.addTab(tab3);
        tabList.addTab(tab4);
        tabList.addTab(tab5);
        tabList.addTab(tab6);
        tabList.setTabLength(200);
        tabList.setTabMargin(20);
        tabList.selectTab(tab1);

        tabList.setFixedMode(false);
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {

                if (tab == tab1) {
                    pageSlider.setCurrentPage(0);
                    pageSliderProvider.selectAndRemove(0, 1);
                } else if (tab == tab2) {
                    pageSlider.setCurrentPage(1);
                    pageSliderProvider.selectAndRemove(1, 1);
                } else if (tab == tab3) {
                    pageSlider.setCurrentPage(2);
                    pageSliderProvider.selectAndRemove(2, 1);
                } else if (tab == tab4) {
                    pageSlider.setCurrentPage(3);
                    pageSliderProvider.selectAndRemove(3, 1);
                } else if (tab == tab5) {
                    pageSlider.setCurrentPage(4);
                    pageSliderProvider.selectAndRemove(4, 1);
                } else if (tab == tab6) {
                    pageSlider.setCurrentPage(5);
                    pageSliderProvider.selectAndRemove(5, 1);
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });


    }


}
